/*
 Copyright (c) 2008 TPSi
 Alex Iskander
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 
 */



#ifndef BINDABLE_BINDABLE_CONVERTER_H_
#define BINDABLE_BINDABLE_CONVERTER_H_

#include "Bindable.h"

namespace Bind
{
	/**
	 \class Converter
	 \brief Converts a property to the specified format.
	 
	 Often, weakly-bound Bound<> Properties will use a converter to convert
	 their target's value to a value of the proper type.
	 
	 There can be \e many converters for a single purpose. For instance, to
	 convert Bound doubles (or other numbers) into strings, there may be several
	 converters; for instance, in addition to the "normal" converter, there
	 may be currency converters, etc. In this way, a converter is more of
	 a \e value \e provider.
	 
	 The actual conversion process is abstracted in case any additional processing
	 may need to be added in the future to the basic converter.
	 
	 ConverterBase, what almost all converters are based on, inherits from
	 Converter, to allow a generic umbrella type to encapsulate all converters.
	 */
	class Converter
	{
	public:
		virtual ~Converter() { }
	};
	
	template <typename Result> class BaseConverter : public Converter
	{
	public:
		/**
		 \fn convert(Bindable *property);
		 \brief Converts the property (if possible) to the desired 
		 
		 This function currently calls the (internal) doConversion method
		 to process the conversion.
		 */
		virtual Result convert(Bindable *property);
		
		/**
		 \fn ~BaseConverter();
		 \brief Destructor.
		 */
		virtual ~BaseConverter() { }
	protected:
		/**
		 \fn doConversion(Bindable *property);
		 \brief Abstract. Does the actual conversion from the Bindable object 
		 to the result type.
		 
		 Derived classes \b \e must implement.
		 */
		virtual Result doConversion(Bindable *property) = 0;
	};
	
	template <typename Result> Result
	BaseConverter<Result>::convert(Bindable *property)
	{
		/* Do conversion on the resolved property */
		return this->doConversion(property);
	}
}

#endif
